<template>
	<view class="columnbox wdh-100 content">
		<view class="top-box wdh-100 rowbox spb">
			<view class="title">抖音/美团团购核销</view>
			<image class="top-img" src="/static/img/verification.png" mode="widthFix"></image>
		</view>
		<view class="v-box columnbox">
			<u-tabs :current='curtype' lineColor='#17698F' :list="typeList" @click="curtype=$event.value"></u-tabs>
			<view class="input-box rowbox">
				<input v-model="code" :placeholder="curtype==0?'请输入抖音核销码':'请输入美团核销码'" />
			</view>
			<view class="wdh-100 rowbox spb btn-list">
				<view class="rowbox record-btn" @click="showRecord">核销记录</view>
				<view class="rowbox send-btn confirm-btn" @click="toVerification">立即核销</view>
			</view>
			<button @click="scanCode" class="send-btn rowbox scan-btn">扫码核销</button>
			<u-divider lineColor='#17698F' text="核销方法" textColor='#000' style="width: 400rpx;"></u-divider>
			<image src="http://mkxd-app.oss-cn-beijing.aliyuncs.com/f0e14e21d24f4d429fad65e1f93f9614.png"
				mode="widthFix" class="wdh-100"></image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				typeList: [{
					name: '抖音核销',
					value: 0
				}, {
					name: '美团核销',
					value: 1
				}],
				curtype: 0,
				code: '',
				submitfalg: false
			};
		},
		methods: {
			scanCode() {
				uni.scanCode({
					success: (res) => {
						if (this.submitfalg) {
							return
						}
						this.submitfalg = true
						uni.showLoading({
							title: '加载中'
						})
						this.$req.post('/xcx/comboWriteOff/dy', {
							code: '',
							encryptedCode: res.result
						}).then(res => {
							this.submitfalg = false
							uni.hideLoading()
							if (res.data.code == 200) {
								uni.showModal({
									title: '核销成功',
									content: '券码核销成功，点击前往使用',
									cancelText: '继续核销',
									confirmText: '使用',
									success: (res) => {
										if (res.confirm) {
											uni.navigateTo({
												url: '/pages/coupon/myCoupon'
											})
										}
									}
								})
							} else {
								uni.showModal({
									title: '核销失败',
									content: res.data.msg
								})
							}
						})

					},
					fail: () => {
						uni.showToast({
							title: '扫码失败',
							icon: 'none'
						})
					}
				})
			},
			showRecord() {
				uni.navigateTo({
					url: './record'
				})
			},
			toVerification() {
				if (this.code == '') {
					uni.showToast({
						title: '请输入核销码',
						icon: 'none'
					})
					return
				} else {
					if (this.submitfalg) {
						return
					}
					this.submitfalg = true
					uni.showLoading({
						title: '加载中'
					})
					this.$req.post('/xcx/comboWriteOff/dy', {
						code: this.code,
						encryptedCode: ''
					}).then(res => {
						this.submitfalg = false
						uni.hideLoading()
						if (res.data.code == 200) {
							this.code = ''
							uni.showModal({
								title: '核销成功',
								content: '券码核销成功，点击前往使用',
								cancelText: '继续核销',
								confirmText: '使用',
								success: (res) => {
									if (res.confirm) {
										uni.navigateTo({
											url: '/pages/coupon/myCoupon'
										})
									}
								}
							})
						} else {
							uni.showModal({
								title: '核销失败',
								content: res.data.msg
							})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background: #f1f4f4;
		min-height: 100vh;
		justify-content: flex-start;
		font-family: PingFang SC;
	}

	.top-box {
		background: #17698F;
		padding: 20rpx 20rpx 70rpx 20rpx;

		& .title {
			color: #fff;
			font-size: 36rpx;
			text-align: left;
			font-weight: bold;
		}

		.top-img {
			width: 300rpx;
			height: 100rpx;
		}
	}

	.v-box {
		background: #fff;
		width: 710rpx;
		padding: 0 20rpx 40rpx 20rpx;
		margin-top: -40rpx;
		z-index: 2;
		border-radius: 20rpx;


		.input-box {
			margin-top: 32rpx;
			background: #edeced;
			width: 100%;
			border-radius: 100rpx;
			height: 70rpx;
			justify-content: flex-start;
			padding: 0 40rpx;

			input {
				font-size: 28rpx;
				width: 100%;
				font-family: PingFang SC;
			}
		}
	}

	.btn-list {
		margin-top: 40rpx;

		.record-btn {
			border: #17698F solid 1px;
			color: #17698F;
			height: 76rpx;
			border-radius: 100rpx;

			font-size: 28rpx;
			width: 200rpx;
		}

		.confirm-btn {
			width: 400rpx;
			font-size: 28rpx;
			height: 76rpx;
			border-radius: 100rpx;
		}
	}

	.scan-btn {
		width: 100%;
		margin-top: 40rpx;
	}
</style>